#include "peer_group.h"

NodePeerGroup::NodePeerGroup(Node *parent, NodePeerGroupParam params) :
    Node(parent)
{
    List<int> tmp_894757184;
    for(int ii=0; ii<params.num_of_peer; ii+=1)
    {
        tmp_894757184.append((params.peer_id_base)+(ii));
    }
    const_peer_ids = tmp_894757184;
    List<NodePeer *> tmp_894757408;
    for(int ii=0; ii<params.num_of_peer; ii+=1)
    {
        NodePeerParam node_param_894757464;
        node_param_894757464.peer_id = const_peer_ids[ii];
        node_param_894757464.softmem_size = params.softmem_size;
        node_param_894757464.num_of_main_memory = params.num_of_main_memory;
        node_param_894757464.num_of_alu = params.num_of_alu;
        node_param_894757464.multiplexer = params.multiplexer;
        node_param_894757464.total_peers = params.total_peers;
        node_param_894757464.base_ptrs = params.base_ptrs;
        tmp_894757408.append(new NodePeer(this, node_param_894757464));
    }
    node_peers = tmp_894757408;
    NodeMRunnerParam node_param_894756680;
    node_param_894756680.peer_ids = const_peer_ids;
    node_mrunner = new NodeMRunner(this, node_param_894756680);
    for(int ii=0; ii<(params.num_of_peer)-(1); ii+=1)
    {
        ChannelLoadFabric::connect(node_peers[ii]->port_chain_out, node_peers[(ii)+(1)]->port_chain_in);
    }
    ChannelLoadFabric::connect(node_peers[-1]->port_chain_out, node_mrunner->port_message_in);
    ChannelLoadFabric::connect(node_mrunner->port_message_out, node_peers[0]->port_chain_in);
    port_chain_out = node_mrunner->port_chain_out;
    port_chain_in = node_mrunner->port_chain_in;
    NodeRouterNanoOpParam node_param_894829568;
    List<List<int>> tmp_894830408;
    for(int temp_884471680=0; temp_884471680<const_peer_ids.size(); temp_884471680+=1)
    {
        int ii = const_peer_ids[temp_884471680];
        List<int> tmp_894830240 = List<int>();
        tmp_894830240.append(ii);
        tmp_894830408.append(tmp_894830240);
    }
    node_param_894829568.routing_map = tmp_894830408;
    node_router_eop = new NodeRouterNanoOp(this, node_param_894829568);
    for(int ii=0; ii<params.num_of_peer; ii+=1)
    {
        ChannelExecOpFabric::connect(node_router_eop->port_senders[ii], node_peers[ii]->port_nano_op_receiver);
    }
    port_nano_op_receiver = node_router_eop->port_receiver;
    List<NodeRouterLoad *> tmp_894831976;
    for(int temp_893341824=0; temp_893341824<params.num_of_main_memory; temp_893341824+=1)
    {
        NodeRouterLoadParam node_param_894831864;
        node_param_894831864.multiplexer = params.multiplexer;
        List<List<int>> tmp_894935168;
        for(int temp_893390632=0; temp_893390632<const_peer_ids.size(); temp_893390632+=1)
        {
            int ii = const_peer_ids[temp_893390632];
            List<int> tmp_894832032 = List<int>();
            tmp_894832032.append(ii);
            tmp_894935168.append(tmp_894832032);
        }
        node_param_894831864.routing_map = tmp_894935168;
        tmp_894831976.append(new NodeRouterLoad(this, node_param_894831864));
    }
    node_router_load = tmp_894831976;
    for(int ii=0; ii<params.num_of_main_memory; ii+=1)
    {
        for(int jj=0; jj<params.num_of_peer; jj+=1)
        {
            ChannelLoadFabric::connect(node_router_load[ii]->port_senders[jj], node_peers[jj]->port_memory_receivers[ii]);
        }
    }
    List<ChannelLoadFabricReceiver *> tmp_894936736;
    for(int ii=0; ii<params.num_of_main_memory; ii+=1)
    {
        tmp_894936736.append(node_router_load[ii]->port_receiver);
    }
    port_memory_receivers = tmp_894936736;
    List<NodeArbiterStore *> tmp_894937520;
    for(int temp_893501960=0; temp_893501960<params.num_of_peer; temp_893501960+=1)
    {
        NodeArbiterStoreParam node_param_894937464;
        node_param_894937464.num_of_receivers = params.num_of_peer;
        tmp_894937520.append(new NodeArbiterStore(this, node_param_894937464));
    }
    node_arbiter_store = tmp_894937520;
    for(int ii=0; ii<params.num_of_main_memory; ii+=1)
    {
        for(int jj=0; jj<params.num_of_peer; jj+=1)
        {
            ChannelStoreFabric::connect(node_peers[jj]->port_memory_senders[ii], node_arbiter_store[ii]->port_receivers[jj]);
        }
    }
    List<ChannelStoreFabricSender *> tmp_894873728;
    for(int ii=0; ii<params.num_of_main_memory; ii+=1)
    {
        tmp_894873728.append(node_arbiter_store[ii]->port_sender);
    }
    port_memory_senders = tmp_894873728;
    NodeAndGateParam node_param_894874400;
    node_param_894874400.num_of_ports = ((params.num_of_peer)+(params.num_of_main_memory))+(1);
    node_halt_detect = new NodeAndGate(this, node_param_894874400);
    for(int ii=0; ii<params.num_of_peer; ii+=1)
    {
        node_peers[ii]->port_halt->connect(node_halt_detect->port_and_ins[ii]);
    }
    for(int ii=0; ii<params.num_of_main_memory; ii+=1)
    {
        node_arbiter_store[ii]->port_halt->connect(node_halt_detect->port_and_ins[(params.num_of_peer)+(ii)]);
    }
    node_mrunner->port_halt->connect(node_halt_detect->port_and_ins[-1]);
    port_halt = node_halt_detect->port_and_out;
}

NodeTaskList NodePeerGroup::tasks() const
{
    NodeTaskList list;
    return list;
}